package com.qk.management.controller;

import com.qk.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.qk.common.util.AliYunOSSOperators;

import java.util.Objects;
import java.util.UUID;

@Slf4j
@RestController
public class FileHandleController {

    @Autowired
    private AliYunOSSOperators oss;

    @PostMapping("/upload")
    public Result upload(MultipartFile image){
      log.info("上传文件:{}",image);
      try {
          Objects.requireNonNull(image,"文件不能为空");
          Objects.requireNonNull(image.getOriginalFilename(),"文件名不能为空");
          String objectname = UUID.randomUUID() + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
          String url = oss.upload(image.getBytes(), objectname);
          return Result.success(url);
      }
      catch (Exception e) {
        log.error("上传文件失败:{}",e.getMessage());
        return Result.error("上传失败");
      }

    }
}
